﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Text.RegularExpressions;

namespace Epic.ComponentServices.Converters
{
    public abstract class LocalizedEnumConverter : EnhancedEnumConverter
    {
        public ResourceManager ResourceManager { get; private set; }
        public CultureInfo Culture { get; set; }

        #region Constructors

        public LocalizedEnumConverter(ResourceManager resourceManager, Type type)
            : base(type)
        {
            ResourceManager = resourceManager;
        }

        public LocalizedEnumConverter(ResourceManager resourceManager, Type type, IEqualityComparer<string> comparer)
            : base(type, comparer)
        {
            ResourceManager = resourceManager;
        }

        public LocalizedEnumConverter(ResourceManager resourceManager, Type type, string separator, IEqualityComparer<string> comparer = null)
            : base(type, separator, comparer)
        {
            ResourceManager = resourceManager;
        }

        public LocalizedEnumConverter(ResourceManager resourceManager, Type type, string separator, Regex parsingSeparator, IEqualityComparer<string> comparer = null)
            : base(type, separator, parsingSeparator, comparer)
        {
            ResourceManager = resourceManager;
        }

        #endregion

        protected virtual string GetResourceKey(FieldInfo fieldInfo)
        {
            return GetResourceKey(fieldInfo.Name);
        }
        protected virtual string GetResourceKey(string enumName)
        {
            return "EnumDisplayName" + EnumType.Name + enumName;
        }

        protected override string GetDisplayName(FieldInfo fieldInfo)
        {
            return ResourceManager.GetString(GetResourceKey(fieldInfo), Culture);
        }
    }
}
